package com.prs.crm.action.stock.flow;

import java.util.Set;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import bsh.This;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.stock.Purchase;
import com.prs.crm.domain.store.Inventory;
import com.prs.crm.domain.store.InventoryDetail;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.stock.InventoryService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.PurchaseService;

public class InventoryFlowAction extends FlowAction<Inventory> {

	private Inventory inventory;
	private InventoryService inventoryService;
	private PaginationSupport pagination;
	private ProductStoreService productStoreService;

	@Action("listInventoryFlow")
	public String listInventoryFlow() {
		pagination = this.getPaginationHelper().prepare(pagination);
		pagination = this.getInventoryService().listFlowTask(Inventory.class,
				pagination, AppContext.getAccountContext().currentUser());
		return SUCCESS;
	}


	@Action("editInventoryFlow")
	public String editInventoryFlow() {
		this.displayEndNodes();
		return SUCCESS;
	}

	
	@Clew(results = { "success" })
	@Action(value = "submitInventoryFlow", results = { @Result(name = "success", type = "redirect", location = "listInventoryFlow") })
	public String submitInventoryFlow() {
		this.getInventoryService().execute(this.getNode(), this.getDetail(),
				inventory, AppContext.getAccountContext().currentUser());
		if (inventory.getFlowType() == 0) {
			
			inventory.setFlowType(1);
		}

		/*
		 * Set<InventoryDetail> inventoryDetails = inventory.getDetails(); for
		 * (InventoryDetail detail : inventoryDetails) { ProductStore
		 * productStore = detail.getProductStore(); String type =
		 * detail.getInventoryType(); if (type.equals("盘盈")) {
		 * productStore.setProductNum
		 * (detail.getNumberInventory()+detail.getInventoryNum()); } else if
		 * (type.equals("盘亏")) {
		 * productStore.setProductNum(detail.getInventoryNum
		 * ()-detail.getNumberInventory()); }
		 * 
		 * }
		 */

		this.setPrompt("提交成功");
		return SUCCESS;
	}

	public Inventory getInventory() {
		return inventory;
	}

	public void setInventory(Inventory inventory) {
		this.inventory = inventory;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public ProductStoreService getProductStoreService() {
		return productStoreService;
	}

	public void setProductStoreService(ProductStoreService productStoreService) {
		this.productStoreService = productStoreService;
	}

}
